package com.appheader.webfragmentviewpagertest.views;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;

/**
 * Created by zhep on 2016/9/28.
 */
public class MyHorizontalScroll extends HorizontalScrollView {
    private int MAX_SCROLL_HEIGHT = 30;
    public MyHorizontalScroll(Context context) {
        super(context);
    }

    public MyHorizontalScroll(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyHorizontalScroll(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {

        int newScrollX = scrollX + deltaX;

        int newScrollY = scrollY + deltaY;

        // Clamp values if at the limits and record
        final int left = -maxOverScrollX - MAX_SCROLL_HEIGHT;
        final int right = maxOverScrollX + scrollRangeX + MAX_SCROLL_HEIGHT;
        final int top = -maxOverScrollY;
        final int bottom = maxOverScrollY + scrollRangeY;

        boolean clampedX = false;
        if (newScrollX > right) {
            newScrollX = right;
            clampedX = true;
        } else if (newScrollX < left) {
            newScrollX = left;
            clampedX = true;
        }

        boolean clampedY = false;
        if (newScrollY > bottom) {
            newScrollY = bottom;
            clampedY = true;
        } else if (newScrollY < top) {
            newScrollY = top;
            clampedY = true;
        }

        onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);

        return clampedX || clampedY;

    }
}
